package com.panshi.qa.provider.modules.controller;
import com.alibaba.fastjson.JSONObject;
import com.panshi.qa.api.vo.ItemQueryVo;
import com.panshi.qa.api.vo.QaQuestion;
import com.panshi.qa.api.vo.QaQuestionUncheck;
import com.panshi.qa.api.vo.QaQuestionFrontdesk;
import com.panshi.qa.provider.modules.domain.*;
import com.panshi.vo.R;
import com.panshi.qa.provider.modules.service.*;
import com.panshi.qa.api.HasbeiQaApp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Api(tags = "问题控制层管理相关接口")
@RestController
@RequestMapping(value = "/api",produces = "application/json")
@CrossOrigin
@Slf4j
public class QuestionController implements HasbeiQaApp {
    @Autowired
    UserCollectService userCollectService;

    @Autowired
    QaQuestionUncheckSystemService qaQuestionUncheckSystemService;

    @Autowired
    QaQuestionSystemService qaQuestionSystemService;

    @Autowired
    QaQuestionUncheckService uncheckQuestionService;

    @Autowired
    QuestionRelevanceService questionRelevanceService;

    @Autowired
    QaQuestionService qaQuestionService;

    @Autowired
    ThTypeService thTypeService;


    @ApiOperation("查询指定问答类型列表")
    @GetMapping("/{typeId}")
    public R question(@PathVariable(name="typeId") Long typeId){
        R r = qaQuestionService.queryIndexTypeQuestionData(typeId);
        log.info(r.getMsg());
       return  r;
    }


    @ApiOperation("查询所有问答列表")
    @GetMapping
    public R question(){
        R r = qaQuestionService.queryIndexQuestionData();
        log.info(r.getMsg());
        return r;
    }

    @ApiOperation("查询指定问题详情")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable(name="id") Long id){
        //查询指定问题详情
        R r = qaQuestionService.queryQuestionDetail(id);
        log.info(r.getMsg());
        return r;
    }


    @ApiOperation("查询指定问题后台详情")
    @GetMapping("/detailSystem/{id}")
    public Map<String,Object> detailSystem(@PathVariable(name="id") Long id){
        //问题对象
        Map<String,Object> map = qaQuestionService.findQuestionOne(id.intValue());
        log.info((String) map.get("msg"));
        return map;
    }



    @ApiOperation("增加指定问题到未审核表")
    @PostMapping("/add")
    public R addUncheckQuestoin(@RequestBody QaQuestionFrontdesk qaQuestionFrontdesk){
        //将提问题增加到未审核表
        R r = uncheckQuestionService.insertQaQuestionUncheck(qaQuestionFrontdesk);
        log.info(r.getMsg());
        return r;
    }

    @ApiOperation("增加指定问题到问题表")
    @PostMapping("/addQuestion")
    public R addQuestoin(@RequestBody  QaQuestionFrontdesk qaQuestionFrontdesk){
        System.out.println("增加的问题      "+qaQuestionFrontdesk);
        //将提问题增加到审核表
        R r = qaQuestionService.insertQaQuestion(qaQuestionFrontdesk);
        log.info(r.getMsg());
        return r;

    }


    @ApiOperation("增加指定问题到收藏表")
    @PostMapping("/addCollect")
    public R addCollect(@RequestBody  QaQuestionFrontdesk qaQuestionFrontdesk){
        //将问题增加到收藏表
        R r = userCollectService.insertCollect(qaQuestionFrontdesk);
        log.info(r.getMsg());
        return r;
    }
    @ApiOperation("查询问答列表的分页")
    @PostMapping(value = "/index/item")
    public R itemQuery(@RequestBody ItemQueryVo itemQueryVo){
        //查询问答列表分页内容
        R r = qaQuestionService.queryIndexItemPage(itemQueryVo);
        log.info(r.getMsg());
        return r;
    }

    @ApiOperation("查询问题口腔类型列表")
    @GetMapping(value = "/index/thtypeQuery")
    public String thtypeQuery(){
        //返回调用者对象
        QuestionResVO resVO = new QuestionResVO();
        //查询所有问答口腔类型
        List<ThType> thTypeList =thTypeService.selectList();
        //存储问答口腔类型到调用者对象
        resVO.setThTypeList(thTypeList);
        return JSONObject.toJSONString(resVO);
    }

    @ApiOperation("查询问答未审核列表的后台")
    @PostMapping(value = "/index/pageUncheckQuery")
    public Map<String,Object> itemUncheckQuery(@RequestBody ItemQueryVo itemQueryVo){
        Map<String,Object> map = qaQuestionUncheckSystemService.queryQuestionUncheckSystem(itemQueryVo);
        log.info((String) map.get("msg"));
        return map;
    }

    @ApiOperation("删除指定问题")
    @DeleteMapping(value = "/index/del/{id}")
    public R delQuestion(@PathVariable("id") int id){
        R r = qaQuestionService.deleteQuestion(id);
        log.info(r.getMsg());
        return r;
    }

    @ApiOperation("删除指定未审核表问题")
    @DeleteMapping(value = "/index/delUncheck/{id}")
    public R delUncheckQuestion(@PathVariable("id") int id){
        R r = uncheckQuestionService.deleteUncheckQuestion(id);
        log.info(r.getMsg());
        return r;
    }


    @ApiOperation("修改审核通过指定问题")
    @PutMapping(value = "/index/updateQuestion")
    public R updateQuestion(@RequestBody QaQuestion qaQuestion){
        R r = qaQuestionService.updateQuestion(qaQuestion);
        log.info(r.getMsg());
        return r;

    }

    @ApiOperation("修改未审核通过指定问题")
    @PutMapping(value = "/index/updateQuestionUncheck")
    public R updateQuestionUncheck(@RequestBody QaQuestionUncheck qaQuestionUncheck){
        try {
            R r = uncheckQuestionService.updateQaQuestionUncheck(qaQuestionUncheck);
            log.info(r.getMsg());
            return r;
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @ApiOperation("查询问答审核列表的后台")
    @PostMapping(value = "/index/pageQuery")
    public Map<String,Object> pageQuery(@RequestBody ItemQueryVo itemQueryVo){

        return qaQuestionSystemService.pageQuerySystem(itemQueryVo);

    }



    @ApiOperation("查询指定问题ID信息")
    @GetMapping("/questionDetail/{id}")
    public R questionDetail(@PathVariable(name="id") Long id){
        //问题对象
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestion = qaQuestionService.selectById(id);

        //返回对象
        QuestionResVO resVO = new QuestionResVO();

        resVO.setQaQuestion(qaQuestion);

        log.info("查询指定问题ID信息成功....");

        return R.ok().data("pageQuery",JSONObject.toJSONString(resVO));
    }

    @ApiOperation("查询首页推荐问答")
    @GetMapping(value="homeQaQuesitonPage")
    public R homeQaQuesitonPage(){
        List<QaQuesitonRelevance> orderList = questionRelevanceService.findHomeQaQuestion();
        //返回对象
        QuestionResVO resVO = new QuestionResVO();
        //首页推荐问答
        resVO.setOrderList(orderList);

        return R.ok().data("orderList",JSONObject.toJSONString(resVO));

    }

    /**
     *  页面传入日期报错问题处理
     * @param binder
     */
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
}
